package yueyong.salarymanagement.module.business.salary.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yueyong.salarymanagement.common.domain.ResponseDTO;
import yueyong.salarymanagement.module.business.salary.dao.BCompanyDao;
import yueyong.salarymanagement.module.business.salary.dao.CompanyDao;
import yueyong.salarymanagement.module.business.salary.domain.dto.CompanyAddDTO;
import yueyong.salarymanagement.module.business.salary.domain.dto.CompanyUpdateDTO;
import yueyong.salarymanagement.module.business.salary.domain.entity.BCompanyEntity;
import yueyong.salarymanagement.module.business.salary.domain.entity.CompanyEntity;
import yueyong.salarymanagement.module.business.salary.domain.vo.CompanyVO;
import yueyong.salarymanagement.module.system.login.domain.RequestTokenBO;
import yueyong.salarymanagement.module.system.role.basic.RoleResponseCodeConst;
import yueyong.salarymanagement.module.system.role.basic.domain.dto.RoleAddDTO;
import yueyong.salarymanagement.module.system.role.basic.domain.dto.RoleUpdateDTO;
import yueyong.salarymanagement.module.system.role.basic.domain.entity.RoleEntity;
import yueyong.salarymanagement.util.SmartBeanUtil;
import yueyong.salarymanagement.util.SmartRequestTokenUtil;

import java.util.List;

/**
 * Copyright: yueyong (c) 2022
 *
 * @author he.shaorong
 * @version 1.0
 * @date 2022/1/26
 */
@Service
public class CompanyService {
    @Autowired
    private CompanyDao companyDao;

    @Autowired
    private BCompanyDao bCompanyDao;


    public List<CompanyVO> queryAllExportData(int companyId) {
        return companyDao.getCompanyById(companyId);
    }

    public List<CompanyVO> queryAllExportData2(int companyId) {

        RequestTokenBO requestToken = SmartRequestTokenUtil.getRequestUser();
        Long uid = requestToken.getRequestUserId();


        return bCompanyDao.getB_CompanyById(companyId);
    }

    public ResponseDTO addCompany(CompanyAddDTO companyAddDTO) {
        BCompanyEntity queryCompany = bCompanyDao.getByCompanyName(companyAddDTO.getCompanyName());
        if (null != queryCompany) {
            return ResponseDTO.wrap(RoleResponseCodeConst.ROLE_NAME_EXISTS);
        }
        BCompanyEntity roleEntity = SmartBeanUtil.copy(companyAddDTO, BCompanyEntity.class);
        bCompanyDao.insert(roleEntity);
        return ResponseDTO.succ();
    }

    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> updateCompany(CompanyUpdateDTO companyUpdateDTO) {
        if (null == bCompanyDao.getB_CompanyById(companyUpdateDTO.getCompanyId())) {
            return ResponseDTO.wrap(RoleResponseCodeConst.ROLE_NOT_EXISTS);
        }

        BCompanyEntity queryCompany = bCompanyDao.getByCompanyName(companyUpdateDTO.getCompanyName());
        if (null == queryCompany) {
            return ResponseDTO.wrap(RoleResponseCodeConst.ROLE_NOT_EXISTS);
        }
        BCompanyEntity bCompanyEntity = SmartBeanUtil.copy(companyUpdateDTO, BCompanyEntity.class);
        bCompanyEntity.setId(queryCompany.getId());
        bCompanyDao.updateById(bCompanyEntity);
        return ResponseDTO.succ();
    }


}
